var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marker:[],
    longitude:"113.324520",
    latitude:"23.099994"
  },
  onLoad: function (options) {
    var that=this;
    var coupons_code = options.coupons_code;
    wx.getSystemInfo({
      success: function (res) {
       that.setData({
         windowHeight: res.windowHeight,
         coupons_code: coupons_code
       })
      }
    })
  },
  onShow: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          lat: res.latitude,
          lng: res.longitude
        })
        that.getShopList();
      }
    })

    
  },

  locationCol:function(e){
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var lat = this.data.dataList[index].lat;
    var lng = this.data.dataList[index].lng;
    this.setData({
      longitude:lng,
      latitude:lat
    })
  },

  //获取店铺列表
  getShopList:function(){
    var that = this;
    var params = new Object()
    params.coupons_code = this.data.coupons_code;
    params.lng = this.data.lng;
    params.lat = this.data.lat;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/useInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var list = res.data.data.list;
          var markers = [];
          if(list.length>0){
          for (var i = 0; i < list.length; i++){

            var marker = {
              id: list[i].shop_id,
              latitude: list[i].lat,
              longitude: list[i].lng,
              iconPath: '../../images/ic_point.png',
              width: 21,
              height: 32
            }

            markers.push(marker)
          }
          that.setData({
            dataList:res.data.data.list,
            markers: markers,
            longitude: list[0].lng,
            latitude: list[0].lat
          })
        }
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  }
})